import ContentPic from "./ContentPic";
import ContentText from "./ContentText";
import GameConfig from "./GameConfig";
import DrawUtil from './DrawUtil'

/**
 * 网格区域
 */
export default class ContentArea extends Laya.Sprite {
    constructor() {
        super();

        this.contentAreaData = {};
        this.tag = ""; // 区域编号
        this.textTipView = null;

        // 区域内图父节点
        this.picRootView = null;
        this.moveRectangle = null;
    }

    getAreaData() {
        return this.contentAreaData;
    }

    initByContentAreaData(inData) {
        this.contentAreaData = inData;
        this.tag = this.contentAreaData.contentAreaId;
        let gridList = this.contentAreaData.areaIndexList;
        let startPosIndex = this.contentAreaData.initPosIndex;

        let startPosXIndex = startPosIndex % 10;
        let startPosYIndex = Math.floor(startPosIndex / 10);

        let startPosX = startPosXIndex * GameConfig.GridWidth;
        let startPosY = startPosYIndex * GameConfig.GridWidth;

        let contentHeightNum = this.contentAreaData.areaHeight;
        let contentWidthNum = this.contentAreaData.areaWidth;

        this.pos(startPosX, startPosY);
        this.size(contentWidthNum * GameConfig.GridWidth, contentHeightNum * GameConfig.GridWidth);
        this.borderRoot = new Laya.Sprite;
        this.borderRoot.size(contentWidthNum * GameConfig.GridWidth, contentHeightNum * GameConfig.GridWidth);
        this.addChild(this.borderRoot);

        // 区域画线
        for (var j = 0; j < gridList.length; j++) {
            let gridindex = gridList[j];
            // 横位置
            let gridXIndex = gridindex % 10;
            // 纵位置
            let gridYIndex = Math.floor(gridindex / 10);
            // 网格中的位置
            let parentIndexX = startPosXIndex + gridXIndex;
            let parentIndexY = startPosYIndex + gridYIndex;

            // left
            if (gridXIndex == 0 || gridList.indexOf(gridindex - 1) == -1)
                this.borderRoot.addChild(DrawUtil.addBorderLine(j,
                    GameConfig.GridWidth * gridXIndex,
                    GameConfig.GridWidth * gridYIndex + GameConfig.elementPadding,
                    1,
                    GameConfig.GridWidth - 2 * GameConfig.elementPadding,
                    this.contentAreaData.borderColor));

            // top
            if (gridYIndex == 0 || gridList.indexOf(gridindex - 10) == -1)
                this.borderRoot.addChild(DrawUtil.addBorderLine(j,
                    GameConfig.GridWidth * gridXIndex + GameConfig.elementPadding,
                    GameConfig.GridWidth * gridYIndex,
                    GameConfig.GridWidth - + GameConfig.elementPadding * 2,
                    1,
                    this.contentAreaData.borderColor));

            // right
            if (gridList.indexOf(gridindex + 1) == -1)
                this.borderRoot.addChild(DrawUtil.addBorderLine(j,
                    GameConfig.GridWidth * (gridXIndex + 1),
                    GameConfig.GridWidth * gridYIndex + GameConfig.elementPadding,
                    1,
                    GameConfig.GridWidth - GameConfig.elementPadding * 2,
                    this.contentAreaData.borderColor));

            // bottom
            if (gridList.indexOf(gridindex + 10) == -1)
                this.borderRoot.addChild(DrawUtil.addBorderLine(j,
                    GameConfig.GridWidth * (gridXIndex + 0) + GameConfig.elementPadding,
                    GameConfig.GridWidth * (gridYIndex + 1),
                    GameConfig.GridWidth - GameConfig.elementPadding * 2,
                    1,
                    this.contentAreaData.borderColor));
        }

        // add text tip
        this.addTextTip();

        this.picRootView = new Laya.Sprite;
        this.addChild(this.picRootView)

        this.moveRectangle = new Laya.Rectangle(startPosX,startPosY,this.width,this.height);

    }

    addTextTip() {
        this.textTipView = new Laya.Text;
        this.addChild(this.textTipView);
        this.textTipView.pos(0, 0);

        this.textTipView.size(this.contentAreaData.areaWidth * GameConfig.GridWidth,
            this.contentAreaData.isTipFirstLine == 0 ? this.contentAreaData.areaHeight * GameConfig.GridWidth : GameConfig.GridWidth);
        this.textTipView.text = this.contentAreaData.title;

        this.textTipView.align = "center";
        this.textTipView.valign = "middle";
        this.textTipView.wordWrap = true;

        this.textTipView.fontSize = GameConfig.GridWidth * 0.3;
        this.textTipView.font = "Microsoft YaHei";
        this.textTipView.color = this.contentAreaData.borderColor;
    }

    addBorderLine(index, posX, posY, hWidth, vWidth, strokeColor = "#0A603F", fillColor = '#ffffff00') {
        let tempLine = new Laya.Sprite;
        tempLine.pos(posX, posY);
        tempLine.width = hWidth;
        tempLine.height = vWidth;

        tempLine.graphics.drawRect(0, 0, hWidth, vWidth, fillColor, strokeColor);
        return tempLine;
    }

    addPicByData(inPicData, inAreaIndex = -1) {
        // ContentPic
        let tempPic = new ContentPic;
        if (inAreaIndex != -1)
            tempPic.initByContentPicData(inAreaIndex, inPicData, this);
        else
            tempPic.initByContentPicData(0, inPicData, this);
        this.picRootView.addChild(tempPic);

        this.parent.updateAreaTips();
    }

    addTextByData(inTextData) {
        if(this.picRootView.numChildren <= 0) {
            let tempText = new ContentText;
            tempText.initByContentTextData(this.contentAreaData, inTextData, this);
            this.picRootView.addChild(tempText);
            this.parent.updateAreaTips();    
        } else {
            this.picRootView.getChildAt(0).updateTextContent(inTextData);
        }
    }

    /**
 * 遍历区域网格中的一个格子为起点，判断是否可以放下输入的网格序列 
 * @param {*} areaData 
 * @param {*} picGridList 
 */
    checkIsHaveEnoughGapInAreaBeforeCreate(picGridList) {
        // let areaInitPosIndex = areaData.initPosIndex;

        let areaIndexList = this.contentAreaData.areaIndexList;

        let re = -1;
        for (var i = 0; i < areaIndexList.length; i++) {
            // 遍历区域内的每个index  以index为起点 检查可以放入新的indexlist
            let areaIndex = areaIndexList[i];
            let re = this.checkIsHaveEnoughGapInAreaPos(areaIndex, picGridList);
            if (re == -1) {
                continue;
            } else {
                return areaIndex;
            }
        }

        return -1;
    }

    /**
     * 判断以某个区域内格子为起点 是否有足够的空间
     * @param {*} areaIndex 区域内的位置作为起始位置
     * @param {*} picGridList 
     * @param {*} exceptPicTag 
     * @returns 
     */
    checkIsHaveEnoughGapInAreaPos(areaIndex, picGridList, exceptPicView) {
        let areaIndexList = this.contentAreaData.areaIndexList;

        // 图片以areaIndex为起点 在区域内的占位list
        let picGridListInArea = [];
        let isAreaHaveGap = true;
        for (var j = 0; j < picGridList.length; j++) {
            picGridListInArea.push(picGridList[j] + areaIndex);
            if (areaIndexList.indexOf(picGridList[j] + areaIndex) < 0) {
                isAreaHaveGap = false;
                break;
            }
        }

        if (isAreaHaveGap == false) {
            return -1;
        }

        let isHaveSet = false;
        for (var m = 0; m < this.picRootView.numChildren; m++) {
            let targetPic = this.picRootView.getChildAt(m);

            if (targetPic === exceptPicView && exceptPicView != null)
                continue;

            let startPosIndex = targetPic.tStartInAreaIndex;

            let isHave = false;
            for (var n = 0; n < targetPic.currentImageGridList.length; n++) {
                let haveSet = picGridListInArea.indexOf(targetPic.currentImageGridList[n] + startPosIndex)
                if (haveSet >= 0) {
                    isHave = true;
                    break;
                }
            }

            if (isHave) {
                isHaveSet = true;
                break
            }
        }
        if (isHaveSet)
            return -1;
        else
            return areaIndex;
    }

    removeContentPic(inView) {
        this.picRootView.removeChild(inView);
        this.parent.updateAreaTips();
    }

    endDragContentPic(nPosX, nPosY, inView) {
        let minDis = Number.MAX_VALUE;
        let nearestGridIndex = -1;

        for (let i = 0; i < this.contentAreaData.areaIndexList.length; i++) {
            // if(inView == this.contentAreaData.areaIndexList[i])
            //     continue
            let gridindex = this.contentAreaData.areaIndexList[i];
            // 横位置
            let gridXIndex = gridindex % 10;
            // 纵位置
            let gridYIndex = Math.floor(gridindex / 10);

            let gridPosX = GameConfig.GridWidth * gridXIndex;
            let gridPosY = GameConfig.GridWidth * gridYIndex;
            let distance = Math.pow((gridPosX - nPosX), 2) + Math.pow((gridPosY - nPosY), 2);
            if (distance < minDis) {
                nearestGridIndex = gridindex;
                minDis = distance;
            }
        }

        let re = this.checkIsHaveEnoughGapInAreaPos(nearestGridIndex, inView.currentImageGridList, inView);
        inView.updatePosByMove(re);
    }

    updateSelectState(exceptView, upToParent) {
        for (let i = 0; i < this.picRootView.numChildren; i++) {
            let temp = this.picRootView.getChildAt(i);
            if (temp == exceptView)
                continue;

            temp.setBoundsAndBtnsVisible(false, false, false);
        }

        if (upToParent)
            this.parent.updateSelectState(this)
    }

    setAreaTipVisible(e) {
        this.textTipView.visible = e;
    }

    setBorderVisible(e) {
        this.borderRoot.visible = e;
    }
}

